# Moderation & Guardrails System

## Overview

The safety and quality architecture is split into two distinct layers:

1.  **Moderation Layer (The Police):** `src/moderation`
    *   Focus: Security, Compliance, Access Control.
    *   Action: **BLOCK** or **REDACT**.
2.  **Guardrails (The Manual):** `src/guardrails`
    *   Focus: Quality, Consistency, Correctness.
    *   Action: **WARN** or **RETRY** (Behavioral guidance).

## 1. Moderation Layer (`src/moderation/manager.py`)

This layer protects the system from malice and protects the user/company from risk.

### Features
*   **Input Moderation:**
    *   🛑 **Toxicity:** Block offensive language.
    *   💉 **Injection:** Block jailbreak attempts (e.g., "Ignore instructions").
    *   👮 **Permissions:** Check if user role allows the query.
    *   🕵️ **PII Masking:** Redact emails/phones before LLM sees them.
*   **Output Moderation:**
    *   📉 **DLP:** Block responses containing secrets or banned words.

### Usage
```python
from src.moderation.manager import ModerationLayer
mod = ModerationLayer()
is_safe, msg, clean_text = mod.moderate_input(text, user_context)
```

## 2. Guardrails (`src/guardrails/validators.py`)

This layer ensures the AI behaves like a good employee.

### Features
*   **Output Validation:**
    *   📖 **Citations:** Warns if factual answers lack source/page references.
    *   🗣️ **Tone Check:** Warns if the assistant is over-apologetic.
    *   ✅ **Format:** (Future) Verify JSON schemas.

### Usage
```python
from src.guardrails.validators import GuardrailsValidator
out_val = GuardrailsValidator()
is_valid, msg, warnings = out_val.validate_output(llm_response, query)
```

## Testing

Run the verification scripts to test both layers:

```bash
# Test Security (Moderation)
export PYTHONPATH=$PYTHONPATH:.
python3 test_moderation.py

# Test Quality (Guardrails)
python3 test_validators.py
```
